import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shenlingwuliu_flutter/pages/login/index.dart';
import 'package:shenlingwuliu_flutter/pages/profile/index.dart';
import 'package:shenlingwuliu_flutter/pages/tab_bar_page.dart';
import 'package:shenlingwuliu_flutter/stores/counter.dart';
import 'package:shenlingwuliu_flutter/utils/tokenManager.dart';

void main() {
  runApp(MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => CounterModel())],
      child: (MaterialApp(
        routes: {
          '/': (context) => const TabBarPage(),
          // '/detail': (context) => const NoticeDetail(),
          '/login': (context) => const LoginPage(),
        },
        onGenerateRoute: (settings) {
          final tokenManager = TokenManager();
          final token = tokenManager.getToken() ?? '';
          if (token.isEmpty && settings.name != '/login') {
            return MaterialPageRoute(builder: (context) => const LoginPage());
          }

          if (settings.name == '/profile') {
            return MaterialPageRoute(builder: (context) => const ProfilePage());
          }

          return MaterialPageRoute(builder: (context) => const TabBarPage());
        },
        initialRoute: '/',
      ))));
}
